package com.xyz.cmsservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyz.entity.R;
import com.xyz.cmsservice.entity.CrmBanner;
import com.xyz.cmsservice.service.CrmBannerService;


import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-06-17
 */
@Schema(description="后台banner接口")
@RestController
@RequestMapping("/cmsservice/banneradmin")
//@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page(page, limit);
        bannerService.page(pageBanner,null);
        long total=pageBanner.getTotal();
        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    @Schema(description="获取Banner")
    @GetMapping("get/{id}")
    public R getBanner(@PathVariable String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item",banner);
    }

    /**
     * 添加banner
     * @return
     */
    @CacheEvict(value = "banner",allEntries=true)//清空
    @Schema(description="添加Banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.ok();
    }

    @CacheEvict(value = "banner",allEntries=true)//清空
    @Schema(description="修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @CacheEvict(value = "banner",allEntries=true)//清空
    @Schema(description="删除Banner")
    @DeleteMapping("{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }
}

